package com.sdry.system.controller;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.*;
import com.sdry.system.service.MenuService;
import com.sdry.untils.UserContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.*;


/**
 * @version: 1.0
 * @description: 菜单
 * @author: cb
 * @create: 2019-12-11 11:32
 **/
@Controller
@RequestMapping("menu")
public class MenuController extends BaseController {

    @Resource
    private MenuService menuService;

    /**
     * 展示页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/showMenuList")
    public String showMenuList(Model model) {
        List<JsonRootData> jsonRootDataList =  menuService.queryMenuNameList();
        model.addAttribute("jsonRootDataList",jsonRootDataList);
        return "/system/menu/menuList";
    }

    /**
     * 修改页面
     * @param permissionId 主键
     * @return
     */
    @RequestMapping(value = "/showUpdateMenu")
    public String showUpdateMenu(Long permissionId,Model model) {
        JsonRootData jsonRootData = menuService.getById(permissionId);
        List<JsonRootData> jsonRootDataList =  menuService.queryMenuNameList();
        model.addAttribute("JsonRootData",jsonRootData);
        model.addAttribute("jsonRootDataList",jsonRootDataList);
        return "/system/menu/update-menu";
    }

    /**
     * 查询所有的菜单
     * @return
     */
    @RequestMapping("queryMenuPage" )
    @ResponseBody
    public TableDataInfo queryMenuPage() {
        QueryWrapper<JsonRootData> queryWrapper = new QueryWrapper<>();
        List<JsonRootData> list = new ArrayList<>();
        queryWrapper.eq("pid",0L).eq("del_flag",0L).orderByAsc("seq");
        List<JsonRootData> list2 = menuService.list(queryWrapper);
        for (JsonRootData jsonRootData : list2) {
            QueryWrapper<JsonRootData> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("pid", jsonRootData.getPermissionId()).eq("del_flag", 0).orderByAsc("seq");
            List<JsonRootData> jsonRootDataList2 = menuService.list(queryWrapper2);
            list.add(jsonRootData);
            list.addAll(jsonRootDataList2);
        }

        Map<String,Object> map = new HashMap<>(16);
        map.put("msg",true);
        map.put("code",0);
        map.put("data",list);
        map.put("count",1000);
        return getDataTable(list, 1000L);
    }

    /**
     * 添加
     * @param jsonRootData
     * @return
     */
    @RequestMapping(value = "/add" )
    @ResponseBody
    public Integer add(JsonRootData jsonRootData,HttpServletRequest request) {
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        jsonRootData.setCreateBy(userId.toString());
        return  menuService.add(jsonRootData);
    }

    /**
     * 修改
     * @param jsonRootData
     * @return
     */
    @RequestMapping(value = "/update" )
    @ResponseBody
    public boolean update(JsonRootData jsonRootData) {
        Long userId = UserContext.getSessionUserIdByServiceImpl();
        jsonRootData.setUpdateBy(userId.toString());
        Timestamp updateDate = new Timestamp(System.currentTimeMillis());
        jsonRootData.setUpdateDate(updateDate);
        return  menuService.saveOrUpdate(jsonRootData);
    }

    /**
     * 删除
     * @param permissionId 主键
     * @return
     */
    @RequestMapping(value = "/delete" )
    @ResponseBody
    public boolean delete(Long permissionId) {
        return menuService.removeById(permissionId);
    }

    /**
     * 进主页菜单&#xe61c;
     * @return
     */
    @RequestMapping("queryMenuList" )
    @ResponseBody
    public String queryMenuList(HttpServletRequest request) {
        //用户
        SysUser sysUser = (SysUser) request.getSession().getAttribute("user");
        if (sysUser == null){
            return null;
        }
        if (sysUser.getUserId() == null){
            return null;
        }
        //当前用户对应的菜单
        List<Long> menuIdList = sysUser.getMenuIdList();
        MenuData menuData = new MenuData();
        List<ContentManagement> contentManagementList = new ArrayList<> ();
        List<JsonRootData> jsonRootDataList = menuService.queryMenuNameList();
        for (JsonRootData jsonRootData : jsonRootDataList) {
            List<Children> childrenList = new ArrayList<> ();
            ContentManagement contentManagement = new ContentManagement();
            contentManagement.setTitle(jsonRootData.getPermissionName());
            contentManagement.setIcon("&#xe705;");
            contentManagement.setHref(jsonRootData.getPermissionUrl());
            contentManagement.setSpread(false);
            contentManagement.setChildren(childrenList);

            List<JsonRootData> jsonRootDataList1 = menuService.queryMenuByPermissionIdList(jsonRootData.getPermissionId());
            for (JsonRootData rootData : jsonRootDataList1) {
                if (menuIdList.contains(rootData.getPermissionId())){
                    Children children = new Children();
                    children.setTitle(rootData.getPermissionName());
                    children.setIcon("&#xe61c;");
                    children.setHref(rootData.getPermissionUrl());
                    children.setSpread(false);
                    childrenList.add(children);
                }
            }
            if (contentManagement.getChildren().size() > 0) {
                contentManagementList.add(contentManagement);
            }
        }
        menuData.setContentManagement(contentManagementList);
        String  object = JSONObject.fromObject(menuData).toString();
        return object;
    }

    /**
     * zTree菜单展示
     * @return
     */
    @RequestMapping("queryMenuRoleList")
    @ResponseBody
    public String queryMenuRoleList() {
        List<JsonRootData> jsonRootDataList = menuService.queryMenuRoleList();
        JSONArray jsonArray = JSONArray.fromObject(jsonRootDataList);
        return jsonArray.toString();
    }

    /**
     * 查询所有选中的菜单
     * @param roleId
     * @return
     */
    @RequestMapping("queryRoleMenuByRoleList")
    @ResponseBody
    public String queryRoleMenuList(Long roleId) {
        List<String> roleMenuDataList = menuService.queryRoleMenuByRoleList(roleId);
        net.sf.json.JSONArray jsonArray = JSONArray.fromObject(roleMenuDataList);
        return jsonArray.toString();
    }

    /**
     * 查询所有选中的app菜单
     * @param roleId
     * @return
     */
    @RequestMapping("queryRoleAppMenuByRoleList")
    @ResponseBody
    public String queryRoleAppMenuByRoleList(Long roleId) {
        List<String> roleMenuDataList = menuService.queryRoleAppMenuByRoleList(roleId);
        net.sf.json.JSONArray jsonArray = JSONArray.fromObject(roleMenuDataList);
        return jsonArray.toString();
    }
}
